<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>errorhandler.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_debug_errorhandler.js.html">errorhandler.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2007 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Error handling utilities.
<a name="line17"></a> *
<a name="line18"></a> */
<a name="line19"></a>
<a name="line20"></a>goog.provide(&#39;goog.debug.ErrorHandler&#39;);
<a name="line21"></a>goog.provide(&#39;goog.debug.ErrorHandler.ProtectedFunctionError&#39;);
<a name="line22"></a>
<a name="line23"></a>goog.require(&#39;goog.asserts&#39;);
<a name="line24"></a>goog.require(&#39;goog.debug&#39;);
<a name="line25"></a>goog.require(&#39;goog.debug.EntryPointMonitor&#39;);
<a name="line26"></a>goog.require(&#39;goog.debug.Trace&#39;);
<a name="line27"></a>
<a name="line28"></a>
<a name="line29"></a>
<a name="line30"></a>/**
<a name="line31"></a> * The ErrorHandler can be used to to wrap functions with a try/catch
<a name="line32"></a> * statement. If an exception is thrown, the given error handler function will
<a name="line33"></a> * be called.
<a name="line34"></a> *
<a name="line35"></a> * When this object is disposed, it will stop handling exceptions and tracing.
<a name="line36"></a> * It will also try to restore window.setTimeout and window.setInterval
<a name="line37"></a> * if it wrapped them. Notice that in the general case, it is not technically
<a name="line38"></a> * possible to remove the wrapper, because functions have no knowledge of
<a name="line39"></a> * what they have been assigned to. So the app is responsible for other
<a name="line40"></a> * forms of unwrapping.
<a name="line41"></a> *
<a name="line42"></a> * @param {Function} handler Handler for exceptions.
<a name="line43"></a> * @constructor
<a name="line44"></a> * @extends {goog.Disposable}
<a name="line45"></a> * @implements {goog.debug.EntryPointMonitor}
<a name="line46"></a> */
<a name="line47"></a>goog.debug.ErrorHandler = function(handler) {
<a name="line48"></a>  goog.base(this);
<a name="line49"></a>
<a name="line50"></a>  /**
<a name="line51"></a>   * Handler for exceptions, which can do logging, reporting, etc.
<a name="line52"></a>   * @type {Function}
<a name="line53"></a>   * @private
<a name="line54"></a>   */
<a name="line55"></a>  this.errorHandlerFn_ = handler;
<a name="line56"></a>};
<a name="line57"></a>goog.inherits(goog.debug.ErrorHandler, goog.Disposable);
<a name="line58"></a>
<a name="line59"></a>
<a name="line60"></a>/**
<a name="line61"></a> * Whether to add tracers when instrumenting entry points.
<a name="line62"></a> * @type {boolean}
<a name="line63"></a> * @private
<a name="line64"></a> */
<a name="line65"></a>goog.debug.ErrorHandler.prototype.addTracersToProtectedFunctions_ = false;
<a name="line66"></a>
<a name="line67"></a>
<a name="line68"></a>/**
<a name="line69"></a> * Enable tracers when instrumenting entry points.
<a name="line70"></a> * @param {boolean} newVal See above.
<a name="line71"></a> */
<a name="line72"></a>goog.debug.ErrorHandler.prototype.setAddTracersToProtectedFunctions =
<a name="line73"></a>    function(newVal) {
<a name="line74"></a>  this.addTracersToProtectedFunctions_ = newVal;
<a name="line75"></a>};
<a name="line76"></a>
<a name="line77"></a>
<a name="line78"></a>/** @override */
<a name="line79"></a>goog.debug.ErrorHandler.prototype.wrap = function(fn) {
<a name="line80"></a>  return this.protectEntryPoint(goog.asserts.assertFunction(fn));
<a name="line81"></a>};
<a name="line82"></a>
<a name="line83"></a>
<a name="line84"></a>/** @override */
<a name="line85"></a>goog.debug.ErrorHandler.prototype.unwrap = function(fn) {
<a name="line86"></a>  goog.asserts.assertFunction(fn);
<a name="line87"></a>  return fn[this.getFunctionIndex_(false)] || fn;
<a name="line88"></a>};
<a name="line89"></a>
<a name="line90"></a>
<a name="line91"></a>/**
<a name="line92"></a> * Private helper function to return a span that can be clicked on to display
<a name="line93"></a> * an alert with the current stack trace. Newlines are replaced with a
<a name="line94"></a> * placeholder so that they will not be html-escaped.
<a name="line95"></a> * @param {string} stackTrace The stack trace to create a span for.
<a name="line96"></a> * @return {string} A span which can be clicked on to show the stack trace.
<a name="line97"></a> * @private
<a name="line98"></a> */
<a name="line99"></a>goog.debug.ErrorHandler.prototype.getStackTraceHolder_ = function(stackTrace) {
<a name="line100"></a>  var buffer = [];
<a name="line101"></a>  buffer.push(&#39;##PE_STACK_START##&#39;);
<a name="line102"></a>  buffer.push(stackTrace.replace(/(\r\n|\r|\n)/g, &#39;##STACK_BR##&#39;));
<a name="line103"></a>  buffer.push(&#39;##PE_STACK_END##&#39;);
<a name="line104"></a>  return buffer.join(&#39;&#39;);
<a name="line105"></a>};
<a name="line106"></a>
<a name="line107"></a>
<a name="line108"></a>/**
<a name="line109"></a> * Get the index for a function. Used for internal indexing.
<a name="line110"></a> * @param {boolean} wrapper True for the wrapper; false for the wrapped.
<a name="line111"></a> * @return {string} The index where we should store the function in its
<a name="line112"></a> *     wrapper/wrapped function.
<a name="line113"></a> * @private
<a name="line114"></a> */
<a name="line115"></a>goog.debug.ErrorHandler.prototype.getFunctionIndex_ = function(wrapper) {
<a name="line116"></a>  return (wrapper ? &#39;__wrapper_&#39; : &#39;__protected_&#39;) + goog.getUid(this) + &#39;__&#39;;
<a name="line117"></a>};
<a name="line118"></a>
<a name="line119"></a>
<a name="line120"></a>/**
<a name="line121"></a> * Installs exception protection for an entry point function. When an exception
<a name="line122"></a> * is thrown from a protected function, a handler will be invoked to handle it.
<a name="line123"></a> *
<a name="line124"></a> * @param {Function} fn An entry point function to be protected.
<a name="line125"></a> * @return {!Function} A protected wrapper function that calls the entry point
<a name="line126"></a> *     function.
<a name="line127"></a> */
<a name="line128"></a>goog.debug.ErrorHandler.prototype.protectEntryPoint = function(fn) {
<a name="line129"></a>  var protectedFnName = this.getFunctionIndex_(true);
<a name="line130"></a>  if (!fn[protectedFnName]) {
<a name="line131"></a>    var wrapper = fn[protectedFnName] = this.getProtectedFunction(fn);
<a name="line132"></a>    wrapper[this.getFunctionIndex_(false)] = fn;
<a name="line133"></a>  }
<a name="line134"></a>  return fn[protectedFnName];
<a name="line135"></a>};
<a name="line136"></a>
<a name="line137"></a>
<a name="line138"></a>/**
<a name="line139"></a> * Helps {@link #protectEntryPoint} by actually creating the protected
<a name="line140"></a> * wrapper function, after {@link #protectEntryPoint} determines that one does
<a name="line141"></a> * not already exist for the given function.  Can be overriden by subclasses
<a name="line142"></a> * that may want to implement different error handling, or add additional
<a name="line143"></a> * entry point hooks.
<a name="line144"></a> * @param {!Function} fn An entry point function to be protected.
<a name="line145"></a> * @return {!Function} protected wrapper function.
<a name="line146"></a> * @protected
<a name="line147"></a> */
<a name="line148"></a>goog.debug.ErrorHandler.prototype.getProtectedFunction = function(fn) {
<a name="line149"></a>  var that = this;
<a name="line150"></a>  var tracers = this.addTracersToProtectedFunctions_;
<a name="line151"></a>  if (tracers) {
<a name="line152"></a>    var stackTrace = goog.debug.getStacktraceSimple(15);
<a name="line153"></a>  }
<a name="line154"></a>  var googDebugErrorHandlerProtectedFunction = function() {
<a name="line155"></a>    if (that.isDisposed()) {
<a name="line156"></a>      return fn.apply(this, arguments);
<a name="line157"></a>    }
<a name="line158"></a>
<a name="line159"></a>    if (tracers) {
<a name="line160"></a>      var tracer = goog.debug.Trace.startTracer(&#39;protectedEntryPoint: &#39; +
<a name="line161"></a>          that.getStackTraceHolder_(stackTrace));
<a name="line162"></a>    }
<a name="line163"></a>    try {
<a name="line164"></a>      return fn.apply(this, arguments);
<a name="line165"></a>    } catch (e) {
<a name="line166"></a>      that.errorHandlerFn_(e);
<a name="line167"></a>      // Re-throw it since this may be expected by the caller.
<a name="line168"></a>      throw new goog.debug.ErrorHandler.ProtectedFunctionError(e);
<a name="line169"></a>    } finally {
<a name="line170"></a>      if (tracers) {
<a name="line171"></a>        goog.debug.Trace.stopTracer(tracer);
<a name="line172"></a>      }
<a name="line173"></a>    }
<a name="line174"></a>  };
<a name="line175"></a>  googDebugErrorHandlerProtectedFunction[this.getFunctionIndex_(false)] = fn;
<a name="line176"></a>  return googDebugErrorHandlerProtectedFunction;
<a name="line177"></a>};
<a name="line178"></a>
<a name="line179"></a>
<a name="line180"></a>/**
<a name="line181"></a> * Installs exception protection for window.setTimeout to handle exceptions.
<a name="line182"></a> */
<a name="line183"></a>goog.debug.ErrorHandler.prototype.protectWindowSetTimeout =
<a name="line184"></a>    function() {
<a name="line185"></a>  this.protectWindowFunctionsHelper_(&#39;setTimeout&#39;);
<a name="line186"></a>};
<a name="line187"></a>
<a name="line188"></a>
<a name="line189"></a>/**
<a name="line190"></a> * Install exception protection for window.setInterval to handle exceptions.
<a name="line191"></a> */
<a name="line192"></a>goog.debug.ErrorHandler.prototype.protectWindowSetInterval =
<a name="line193"></a>    function() {
<a name="line194"></a>  this.protectWindowFunctionsHelper_(&#39;setInterval&#39;);
<a name="line195"></a>};
<a name="line196"></a>
<a name="line197"></a>
<a name="line198"></a>/**
<a name="line199"></a> * Helper function for protecting setTimeout/setInterval.
<a name="line200"></a> * @param {string} fnName The name of the function we&#39;re protecting. Must
<a name="line201"></a> *     be setTimeout or setInterval.
<a name="line202"></a> * @private
<a name="line203"></a> */
<a name="line204"></a>goog.debug.ErrorHandler.prototype.protectWindowFunctionsHelper_ =
<a name="line205"></a>    function(fnName) {
<a name="line206"></a>  var win = goog.getObjectByName(&#39;window&#39;);
<a name="line207"></a>  var originalFn = win[fnName];
<a name="line208"></a>  var that = this;
<a name="line209"></a>  win[fnName] = function(fn, time) {
<a name="line210"></a>    // Don&#39;t try to protect strings. In theory, we could try to globalEval
<a name="line211"></a>    // the string, but this seems to lead to permission errors on IE6.
<a name="line212"></a>    if (goog.isString(fn)) {
<a name="line213"></a>      fn = goog.partial(goog.globalEval, fn);
<a name="line214"></a>    }
<a name="line215"></a>    fn = that.protectEntryPoint(fn);
<a name="line216"></a>
<a name="line217"></a>    // IE doesn&#39;t support .call for setInterval/setTimeout, but it
<a name="line218"></a>    // also doesn&#39;t care what &quot;this&quot; is, so we can just call the
<a name="line219"></a>    // original function directly
<a name="line220"></a>    if (originalFn.call) {
<a name="line221"></a>      return originalFn.call(this, fn, time);
<a name="line222"></a>    } else {
<a name="line223"></a>      return originalFn(fn, time);
<a name="line224"></a>    }
<a name="line225"></a>  };
<a name="line226"></a>  win[fnName][this.getFunctionIndex_(false)] = originalFn;
<a name="line227"></a>};
<a name="line228"></a>
<a name="line229"></a>
<a name="line230"></a>/** @override */
<a name="line231"></a>goog.debug.ErrorHandler.prototype.disposeInternal = function() {
<a name="line232"></a>  // Try to unwrap window.setTimeout and window.setInterval.
<a name="line233"></a>  var win = goog.getObjectByName(&#39;window&#39;);
<a name="line234"></a>  win.setTimeout = this.unwrap(win.setTimeout);
<a name="line235"></a>  win.setInterval = this.unwrap(win.setInterval);
<a name="line236"></a>
<a name="line237"></a>  goog.base(this, &#39;disposeInternal&#39;);
<a name="line238"></a>};
<a name="line239"></a>
<a name="line240"></a>
<a name="line241"></a>
<a name="line242"></a>/**
<a name="line243"></a> * Error thrown to the caller of a protected entry point if the entry point
<a name="line244"></a> * throws an error.
<a name="line245"></a> * @param {*} cause The error thrown by the entry point.
<a name="line246"></a> * @constructor
<a name="line247"></a> * @extends {goog.debug.Error}
<a name="line248"></a> */
<a name="line249"></a>goog.debug.ErrorHandler.ProtectedFunctionError = function(cause) {
<a name="line250"></a>  var message = goog.debug.ErrorHandler.ProtectedFunctionError.MESSAGE_PREFIX +
<a name="line251"></a>      (cause &amp;&amp; cause.message ? String(cause.message) : String(cause));
<a name="line252"></a>  goog.base(this, message);
<a name="line253"></a>
<a name="line254"></a>  /**
<a name="line255"></a>   * The error thrown by the entry point.
<a name="line256"></a>   * @type {*}
<a name="line257"></a>   */
<a name="line258"></a>  this.cause = cause;
<a name="line259"></a>
<a name="line260"></a>  var stack = cause &amp;&amp; cause.stack;
<a name="line261"></a>  if (stack &amp;&amp; goog.isString(stack)) {
<a name="line262"></a>    this.stack = /** @type {string} */ (stack);
<a name="line263"></a>  }
<a name="line264"></a>};
<a name="line265"></a>goog.inherits(goog.debug.ErrorHandler.ProtectedFunctionError, goog.debug.Error);
<a name="line266"></a>
<a name="line267"></a>
<a name="line268"></a>/**
<a name="line269"></a> * Text to prefix the message with.
<a name="line270"></a> * @type {string}
<a name="line271"></a> */
<a name="line272"></a>goog.debug.ErrorHandler.ProtectedFunctionError.MESSAGE_PREFIX =
<a name="line273"></a>    &#39;Error in protected function: &#39;;
</pre>


</body>
</html>
